package com.hnrd.crud.shiro;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.springframework.stereotype.Component;
/**
 * 自定义session的增删改查
 * @author Admin
 *
 */

@Component
public class MyshiroSessionDao implements SessionDAO{
	public static Map<String, Object> map= new HashMap<>();
	
	@Override
	public Serializable create(Session session) {
		// TODO Auto-generated method stub
		System.out.println("自定义session创建");
		UUID sessionId = UUID.randomUUID();
		session.setAttribute(sessionId, session);
		session.setTimeout(1000);
		map.put(sessionId.toString(), session);
		System.out.println(sessionId);
		return sessionId;
	}

	@Override
	public Session readSession(Serializable sessionId) throws UnknownSessionException {
		// TODO Auto-generated method stub
		System.out.println("获取session");
		return (Session)map.get(sessionId);
	}

	@Override
	public void update(Session session) throws UnknownSessionException {
		System.out.println("修改session");
		session.setAttribute(UUID.randomUUID(), session);
		System.err.println("sessionID:"+session.getId());
		session.setTimeout(1000);
		map.put(session.getId().toString(),session);
	}

	@Override
	public void delete(Session session) {
		map.remove(session.getId(),session);
	}

	@Override
	public Collection<Session> getActiveSessions() {
		// TODO Auto-generated method stub
		System.out.println("获取所有的存活的session");
		List<Session> list = new ArrayList<>();
		map.forEach((k,v)->list.add((Session) v));
		return list;
	}
}
